package com.javaAdmin.interceptor;

import cn.hutool.core.util.StrUtil;
//import com.zxdemo.anon.NeedToken;
import com.javaAdmin.anon.NeedToken;
import com.javaAdmin.exection.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 全局拦截器
 * @author Administrator
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {

    private static Logger log = LoggerFactory.getLogger(TokenInterceptor.class);


    /**
     * 当返回true 执行controller 目标方法
     * 当返回false 的时候 controller 目标方法   postHandle afterCompletion 均不执行
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //检测是否有 needToken 注解，有的话直接判断tokne
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            NeedToken needToken = handlerMethod.getMethod().getAnnotation(NeedToken.class);
            if (needToken != null) {
                //判断token
                String token = request.getHeader("token");
                System.out.println(token);
                if (StrUtil.isBlank(token)) {
                    throw new BusinessException("token不能为空");
                }
                if (!"51".equals(token)) {
                    throw new BusinessException("token错误");
                }
            }
        }


        System.out.println("拦截器第一步：preHandle");

        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
     * controlerr方法执行之后
     * 并且视图未渲染之前进行调用
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //System.out.println("第二部：controlerr方法执行之后");

        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }


    /**
     * 在整个请求处理完毕之后进行回调
     * 可以用来处理线程在资源的释放
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //System.out.println("第三步：线程结束资源释放");

        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

